'use strict';

const xtpl = require('xtpl');
const path = require('path');
const md5 = require('md5');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));


module.exports.projectList = function(req,res){
    //判断是否登录
    CommonHelper.isLogin(req,res);
    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 10;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('project').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('project').alias('project').join({
            table: 'school',
            join: 'left',
            as: 'type',
            on: ['school_id', 'school_id']
        }).where(where).order('project.pro_id DESC').limit(start,limit).select().then(function(project_list){

            let data = {
                array:project_list,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"projectList.html",data);
        });
    });
};

module.exports.projectAdd = function(req,res){
    //判断是否登录
    CommonHelper.isLogin(req,res);

    db.table('school').select().then(function(school){
        db.table('project').select().then(function(project_list){
            let Data = {
                "school_list":school,
                "project_list":project_list
            };

            CommonHelper.renderFile(req,res,"projectAdd.html",Data);
        });
    });        
};

module.exports.projectAddForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    //先获取表单的数据

    let data = {
        "pro_name":req.body.pro_name,
        "pro_type":req.body.pro_type,
        "school_id":req.body.school_id
    };

    db.table('project').add(data).then(function(insertId){
        if(insertId)
        {
            //重定向
            res.redirect(`/project/projectList`);
            return false;
        }else{
            //重定向
            res.redirect(`/project/projectAdd`);
            return false;
        }
    });
};

module.exports.projectEdit = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let pro_id = req.query.pro_id;

    if(!pro_id)
    {
        //重定向
        res.redirect(`/project/projectList`);
        return false;
    }

    db.table('project').where(`pro_id = ${pro_id}`).find().then(function(project){
        db.table('school').select().then(function(school_list){
            let Data = {
                "project":project,
                "school_list":school_list
            };

            CommonHelper.renderFile(req,res,"projectEdit.html",Data);
        });
    });
};

module.exports.projectEditForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let pro_id = req.query.pro_id;

    if(!pro_id)
    {
        //重定向
        res.redirect(`/project/projectList`);
        return false;
    }

    db.table('project').where(`pro_id = ${pro_id}`).find().then(function(project){

        let data = {
            "pro_name":req.body.pro_name,
            "pro_type":req.body.pro_type,
            "school_id":req.body.school_id
        };

        db.table('project').where(`pro_id = ${pro_id}`).update(data).then(function(affectId){
            if(affectId)
            {
                //重定向
                res.redirect(`/project/projectList`);
                return false;
            }else{
                //重定向
                res.redirect(`/project/projectEdit?pro_id=${pro_id}`);
                return false;
            }
        });
    });
};

module.exports.projectDelete = function(req,res){

    let pro_id = req.query.pro_id;

    if(!pro_id)
    {
        //重定向
        res.redirect(`/project/projectList`);
        return false;
    }
    
    db.table('project').where(`pro_id = ${pro_id}`).delete().then(function(affectId){
        if(affectId){
            console.log('删除成功');
            //重定向
            res.redirect(`/project/projectList`);
            return false;
        }else{
            console.log('删除失败');
            //重定向
            res.redirect(`/project/projectList`);
            return false;
        }
    });
};